Built-in R Features - Math

We've talked a bit about some of the built-in math functions and features in R, but let's have one more look at a few of them:

  • abs(): computes the absolute value.
  • sum(): returns the sum of all the values present in the input.
  • mean(): computes the arithmetic mean.
  • round(): rounds values (additional arguments to nearest)

Here's a quick example of each:

In [7]:
v <- c(-1,0,1,2,3,4,5)
In [8]:
abs(-2)
Out[8]:
2
In [9]:
abs(v)
Out[9]:
  1. 1
  2. 0
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
In [10]:
sum(v)
Out[10]:
14
In [11]:
mean(v)
Out[11]:
2
In [13]:
round(23.1231)
Out[13]:
23
In [14]:
round(23.1231234,2)
Out[14]:
23.12

Want more built-in math functions? Check out:

Numeric Functions

Function Description
abs(x) absolute value
sqrt(x) square root
ceiling(x) ceiling(3.475) is 4
floor(x) floor(3.475) is 3
trunc(x) trunc(5.99) is 5
round(x, digits=n) round(3.475, digits=2) is 3.48
signif(x, digits=n) signif(3.475, digits=2) is 3.5
cos(x), sin(x), tan(x) also acos(x), cosh(x), acosh(x), etc.
log(x) natural logarithm
log10(x) common logarithm
exp(x) e^x

You can also check out the documentation for even more functions, like those related to statistics (we'll cover those later on).